package com.best.oasis.settlement.util;

import java.util.Properties;

import javax.naming.Context;
import javax.naming.ldap.InitialLdapContext;
import javax.naming.ldap.LdapContext;

public class LDAPUtil {

	 public static final String USER_TYPE_BEST= "USER_TYPE_BEST";
	 public static final String USER_TYPE_HT= "USER_TYPE_HT";
	 
	/**
	 * 
	 * 用户域验证
	 * @param userName
	 * @param passwd
	 * @return
	 */
	public static boolean validateByLdap(String userName, String passwd) {

		String INITIAL_CONTEXT_FACTORY = "com.sun.jndi.ldap.LdapCtxFactory";

		String PROVIDER_URL = "ldap://800best.net:389";

		String SECURITY_AUTHENTICATION = "simple";

		String domain = "";
		if( getUserType(userName) == USER_TYPE_HT ){
			//汇通员工
			domain = "ht.800best.net";
		}else{
			//best员工
			domain = "800best";
		}
		System.out.println("userName:" + userName + " domain:" + domain );
		
		Properties env = new Properties();

		env.put(Context.INITIAL_CONTEXT_FACTORY, INITIAL_CONTEXT_FACTORY);

		env.put(Context.PROVIDER_URL, PROVIDER_URL);

		env.put(Context.SECURITY_AUTHENTICATION, SECURITY_AUTHENTICATION);

		env.put(Context.SECURITY_PRINCIPAL, userName + "@" + domain);

		if (passwd == null || passwd.length() == 0){
			env.put(Context.SECURITY_CREDENTIALS,"SECUR9d9dITY_AU9o292THENTICATION");
		}
		else{
			env.put(Context.SECURITY_CREDENTIALS, passwd);
		}
		try {

			LdapContext context = new InitialLdapContext(env, null);
			return true;

		} catch (Exception e) {

			System.out.println(e);

			return false;

		}

	}
	/**
	 * 根据用户名选择员工类型
	 * @param userName
	 * @return
	 */
	public static String getUserType(String userName ){
		String result = USER_TYPE_BEST;
		userName= userName.toUpperCase();
		if( userName.indexOf("BLH") >=0  || userName.indexOf("OSH") >=0 || userName.indexOf("TMH") >=0 ){
			//汇通员工
			result = USER_TYPE_HT;
		}else{
			//best员工
			result = USER_TYPE_BEST;
		}
		return result;
	}
	
	public static String getUrl( String userName ) {
		String url= "";
		if( getUserType( userName ) == USER_TYPE_HT ){
			url = Config.getHtLdapUrl();
		}else{
			//url = Config.getBestLdapUrl(); 
			url = "ldap://besthz1.800best.net:389/dc=800best,dc=net ldap://besthz3.800best.net:389/dc=800best,dc=net";
		}
		url = url.replace(" dc=", "dc=");
		url = url.replace("[", "");
		url = url.replace("]", "");
		return url;
	}
	
	public static String getUserDn( String userName ) {
		String userDn= "";
		if( getUserType( userName ) == USER_TYPE_HT ){
			userDn = Config.getHtLdapUserDn();
		}else{
			userDn = Config.getBestLdapUserDn();
			
		}
		return userDn;
	}
	
	public static String getPassword( String userName ) {
		String password= "";
		if( getUserType( userName ) == USER_TYPE_HT ){
			password = Config.getHtLdapPassword();
		}else{
			password = Config.getBestLdapPassword();
		}
		return password;
	}
	
	public static String getSearchFilter() {
		String filter= "";
		filter = Config.getSearchFilter();
		return filter;
	}
	
	public static void main(String[] args) {
		System.out.println(validateByLdap("BL00575", "xinzi52"));
		
		System.out.println(validateByLdap("BLH01054", "110104"));
	}
}
